package com.ruoyi.lowcode.database;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.lowcode.domain.LcDatabaseSource;
import com.ruoyi.lowcode.domain.bo.LcDatabaseSourceBo;
import com.ruoyi.lowcode.domain.vo.LcDatabaseSourceVo;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;
import java.util.*;

@Slf4j
public class MysqlSource implements SourceFactory {

    @Override
    public void connection(LcDatabaseSourceBo bo) throws ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://" + bo.getIp() + ":" + bo.getPort() + "/" + bo.getDatabaseName() + "?" + bo.getAttribute();
        try {
            Connection conn = DriverManager.getConnection(url, bo.getUsername(), bo.getPassword());
            conn.close();
        } catch (SQLException e) {
            log.error("测试连接失败，”" + e.getMessage() + "“");
            throw new RuntimeException("连接失败，”" + e.getMessage() + "“");
        }
    }

    @Override
    public List<Map<String, Object>> selectList(LcDatabaseSourceVo databaseSource, String sql) throws ClassNotFoundException {

        List<Map<String, Object>> data = new ArrayList<>();

        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://" + databaseSource.getIp() + ":" + databaseSource.getPort() + "/" + databaseSource.getDatabaseName() + "?" + databaseSource.getAttribute();

        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        try {
            conn = DriverManager.getConnection(url, databaseSource.getUsername(), databaseSource.getPassword());
            st = conn.createStatement();
            rs = st.executeQuery(sql);

            ResultSetMetaData rsMetaData = rs.getMetaData();

            while(rs.next()){
                Map<String,Object> map = new LinkedHashMap<>();
                for(int i = 1 ; i <= rsMetaData.getColumnCount() ; i++){
                    String colName = rsMetaData.getColumnLabel(i);
                    map.put(colName, rs.getObject(colName));
                }
                data.add(map);
            }

            return data;
        } catch (SQLException e) {
            log.error("查询失败，”" + e.getMessage() + "“");
            throw new RuntimeException("查询失败，”" + e.getMessage() + "“");
        } finally {
            try {
                if(ObjectUtil.isNotNull(rs)){
                    rs.close();
                }
                if(ObjectUtil.isNotNull(st)){
                    st.close();
                }
                if(ObjectUtil.isNotNull(conn)){
                    conn.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public int update(LcDatabaseSourceVo databaseSource, String sql) throws ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://" + databaseSource.getIp() + ":" + databaseSource.getPort() + "/" + databaseSource.getDatabaseName() + "?" + databaseSource.getAttribute();
        try {

            Connection conn = DriverManager.getConnection(url, databaseSource.getUsername(), databaseSource.getPassword());
            Statement st = conn.createStatement();
            int rs = st.executeUpdate(sql);

            st.close();
            conn.close();
            return rs;
        } catch (SQLException e) {
            log.error("更新失败，”" + e.getMessage() + "“");
            throw new RuntimeException("更新失败，”" + e.getMessage() + "“");
        }
    }

}
